package com.dz.order.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.dz.basic.entity.LoanQualification;
import com.dz.basic.entity.RelationQualification;
import com.dz.common.context.FacadeContext;
import com.dz.common.context.UserContextVO;
import com.dz.customer.entity.Customer;
import com.dz.order.OrderVO;
import com.dz.order.entity.Order;
import com.dz.order.entity.OrderProgressEnum;
import com.dz.order.entity.OrderStateEnum;
import com.dz.order.mapper.OrderDao;
import com.dz.order.service.CustomerService;
import com.dz.order.service.OrderService;
import com.dz.order.service.RelationQualificationService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * <p>
 * 订单基础表 服务实现类
 * </p>
 *
 * @author star
 * @since 2017-06-27
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderDao, Order> implements OrderService {

	@Autowired
	private CustomerService customerService;

	@Autowired
	private RelationQualificationService relationQualificationService;

	private Customer updateCustomer(OrderVO entity) {
		UserContextVO userContextVO = FacadeContext.getCurrentUser();
		Long customerId = userContextVO.getCustId();
		String name = entity.getCustomerName();
		Customer customer = new Customer();
		customer.setId(customerId);
		customer.setName(name);
		customer.setMobile(entity.getMobile());
		customerService.updatePhone(customer);
		return customer;
	}

	@Transactional
	public Order addOrder(OrderVO entity) {
		Customer customer = updateCustomer(entity);
		Order order = new Order();
		BeanUtils.copyProperties(entity, order);
		order.setOrderStateid(OrderStateEnum.submit.state);
		order.setOrderStateDesc(OrderStateEnum.submit.desc);
		order.setOrderno(generateNO());
		order.setCreateBy("wx");
		order.setCustomerid(customer.getId());
		order.setCustomerName(customer.getName());
		order.setApplyDate(new Date());
		order.setCreateDate(new Date());
		order.setOrderProgressid(OrderProgressEnum.APPLYED.state);
		order.setOrderProgressDesc(OrderProgressEnum.APPLYED.desc);
		insert(order);
		return order;
	}

	private String generateNO() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		return dateFormat.format(new Date()) + RandomStringUtils.randomNumeric(7);
	}

	public boolean confirmOrder(OrderVO entity) {
		Long orderId = entity.getId();
		List<LoanQualification> loanQualifications = entity.getLoanQualifications();
		Assert.notEmpty(loanQualifications, "资质数据不能为空");
		List<RelationQualification> relationQualifications = new ArrayList<RelationQualification>();
		for (LoanQualification loanQualification : loanQualifications) {
			RelationQualification relationQualification = new RelationQualification();
			relationQualification.setItemid(orderId);
			relationQualification.setItemClassid(ORDER_CLASS_ID.intValue());
			BeanUtils.copyProperties(loanQualification, relationQualification);
			relationQualifications.add(relationQualification);
		}
		boolean flag = relationQualificationService.addBatch(relationQualifications);
		if (flag) {
			Wrapper<Order> orderWrapper = new EntityWrapper<Order>();
			orderWrapper.eq("fid", entity.getId());
			Order update = new Order();
			update.setUpdateDate(new Date());
			update.setOrderStateid(OrderStateEnum.handing.state);
			update.setOrderStateDesc(OrderStateEnum.handing.desc);
			update(update, orderWrapper);
		}
		return flag;
	}
}
